package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.service.system.Login2Service;
import org.apache.commons.io.FileUtils;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

@RestController
@RequestMapping("/image")
public class ImageController {

    @Reference
    private Login2Service login2Service;

    //@Secured( value={"ROLE_ANONYMOUS"})
    //@CrossOrigin
    @RequestMapping(value="/receive2",method= RequestMethod.POST)
    @ResponseBody
    public String base64UpLoad(String base64Data, HttpServletRequest request, HttpServletResponse response){
        System.out.println("-----------------------"+base64Data);
        try{
            String dataPrix = "";
            String data = "";
            if(base64Data == null || "".equals(base64Data)){
                throw new Exception("上传失败，上传图片数据为空");
            }else{
                String [] d = base64Data.split("base64,");
                if(d != null && d.length == 2){
                    dataPrix = d[0];
                    data = d[1];
                }else{
                    throw new Exception("上传失败，数据不合法");
                }
            }
            String suffix = "";
            if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){//编码的jpeg图片数据
                suffix = ".jpg";
            } else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){//编码的icon图片数据
                suffix = ".ico";
            } else if("data:image/gif;".equalsIgnoreCase(dataPrix)){//编码的gif图片数据
                suffix = ".gif";
            } else if("data:image/png;".equalsIgnoreCase(dataPrix)){//编码的png图片数据
                suffix = ".png";
            }else{
                throw new Exception("上传图片格式不合法");
            }
            //String tempFileName = UUID.randomUUID().toString() + suffix;
            String tempFileName = "123" + suffix;

            //因为BASE64Decoder的jar问题，此处使用spring框架提供的工具包
            byte[] bs = Base64Utils.decodeFromString(data);
            try{
                //使用apache提供的工具类操作流
                System.out.println(request.getSession().getServletContext().getRealPath("/picture"));
                FileUtils.writeByteArrayToFile(new File(request.getSession().getServletContext().getRealPath("/picture"), tempFileName), bs);
            }catch(Exception ee){
                throw new Exception("上传失败，写入文件失败，"+ee.getMessage());
            }

            String picture = request.getSession().getServletContext().getRealPath("/picture");
            String compare =request.getSession().getServletContext().getRealPath("/compare");

            Integer score = ImageUtil.compareImage(picture+"/123.png", compare+"/123.png");
            System.out.println("人脸匹配度:"+score+"%");
            login2Service.save(score);


            //比对图片后删除文件
            File srcfile = new File(picture+"/123.png");
            srcfile.delete();
//            File[] files = srcfile.listFiles();
//            delDir(files);


            return "success";
        }catch (Exception e) {
            return "error";
        }

    }
//    public void delDir(File[] files){
//        //遍历FIle对象得到目录下的所有文件对象包含文件夹
//        for (File file : files) {
//            //如果是个文件夹就调用自己,继续遍历
//            if(file.isDirectory()){
//                //如果是文件夹就调用自己的方法删除文件
//                delDir(file.listFiles());
//                //等文件都删除了,再把这个文件夹删了
//                file.delete();
//            }else{
//                //如果是文件就删除
//                file.delete();
//            }
//        }
//    }











}